var TimeControl = function() {
	this._actionQueue = [];
	this._pause = false;
	this._index = 0;
	this._counter = 0;
	this._totalDuring = 0;
	this._progressBar;
	this._done = false;
	this._allObjs = [];
}
TimeControl.prototype.setProgressBar = function(pb) {
	this._progressBar = pb;
}
TimeControl.prototype.adjustBar = function() {
	if (typeof this._progressBar != 'undefined') {
		this._progressBar.set({
			vs : [
			// start vertex position
			[ 0, 560 ],
			// end vertex position
			[ (this._counter / this._totalDuring) * 750, 560 ] ]
		});
	}
}
TimeControl.prototype.push2AllObjs = function(obj) {
	this._allObjs.push(obj);
}
TimeControl.prototype.add = function(objs) {
	objs.during += this._totalDuring;
	this._totalDuring = objs.during;
	this._actionQueue.push(objs);
}
TimeControl.prototype.forward = function() {
	if (this._index >= 0 && this._index < this._actionQueue.length) {
		var paused = this._pause;
		this.pause();
		for (var i = 0; i < this._actionQueue[this._index].objs.length; i++) {
			this._actionQueue[this._index].objs[i].forwardAction();
		}
		this._counter = this._actionQueue[this._index].during;
		this._index++;
		if (!paused)
			this.start();
		this.adjustBar();
		if (this._index >= this._actionQueue.length) {
			this.showReplay();
		}
	} else if(this._index >= this._actionQueue.length){
		var url = window.location.href;
		if(url.indexOf('/chain')!=-1){
			if(url.indexOf('?')!=-1){
				window.location.href=url.substring(0,url.indexOf('?'))+'?handplayid='+recordHelper.handplayid;
			}
			else
				window.location.href=url+'?handplayid='+recordHelper.handplayid;
		} else {
			this.showReplay();
		}
	}
}
TimeControl.prototype.backward = function() {
	if (this._index > 0) {
		var paused = this._pause;
		this.pause();
		//跳过结尾等待
		if(this._index == this._actionQueue.length){
			this._index--;
			for (var i = 0; i < this._actionQueue[this._index].objs.length; i++) {
				this._actionQueue[this._index].objs[i].backwardAction();
			}
		}
		this._index--;
		for (var i = 0; i < this._actionQueue[this._index].objs.length; i++) {
			this._actionQueue[this._index].objs[i].backwardAction();
		}


		if (!paused)
			this.start();

	}
	if (this._index > 0)
		this._counter = this._actionQueue[this._index - 1].during;
	else {
		this._counter = 0;
	}
	this.adjustBar();
}
TimeControl.prototype.restart = function() {
	this.pause();
	this._index = 0;
	for (var i = 0; i < this._allObjs.length; i++) {
		this._allObjs[i].restart();
	}
	this._counter = 0;
	this.start();
	this._done = false;
	this.adjustBar();
}

TimeControl.prototype.start = function() {
	this._pause = false;
}
TimeControl.prototype.pause = function() {
	this._pause = true;
}

TimeControl.prototype.onUp = function() {
	if (!this._pause) {
		if (this._counter < this._totalDuring) {
			this._counter++;
			if (this._index >= 0 && this._index < this._actionQueue.length) {
				if (this._counter == this._actionQueue[this._index].during) {
					for (var i = 0; i < this._actionQueue[this._index].objs.length; i++) {
						this._actionQueue[this._index].objs[i].forwardAction();
					}
					this._index++;
				}
			} else {
				this.pause();
				var url = window.location.href;
				if(url.indexOf('/chain')!=-1){
					console.log(1);
					if(url.indexOf('?')!=-1){
						console.log(url.substring(0,url.indexOf('?'))+'?handplayid='+recordHelper.handplayid);
						window.location.href=url.substring(0,url.indexOf('?'))+'?handplayid='+recordHelper.handplayid;
					}
					else
						window.location.href=url+'?handplayid='+recordHelper.handplayid;
				}else {
					this.showReplay();
				}
			}
			// ���½����
			this.adjustBar();
		} else {
			this.pause();
			var url = window.location.href;
			if(url.indexOf('/chain')!=-1){
				if(url.indexOf('?')!=-1){
					window.location.href=url.substring(0,url.indexOf('?'))+'?handplayid='+recordHelper.handplayid;
				}
				else
					window.location.href=url+'?handplayid='+recordHelper.handplayid;
			}else {
				this.showReplay();
			}
		}
	}
}
TimeControl.prototype.showReplay = function() {
	if (!this._done) {
		this._done = true;
		if(recordHelper.showReplayButton)
			recordHelper.showReplayButton();
	}
}

var recordHelper = {
	chipsFormat : function(chips) {
		var chips = parseFloat(chips), ret = chips ,temp = chips;
		if(chips<0){
			chips = -chips;
		}
		if (1000 < chips && chips < 1000000) {
			c_a = Math.floor(chips / 1000);
			if(c_a>10)
				ret = c_a + 'k';
			else {
				c_b = Math.round((chips-c_a*1000)/100);
				if(c_b==10){
					c_b=9;
				}
				ret = c_a + '.' + c_b + 'k';

			}
		} else if (chips > 1000000) {
			c_a = Math.floor(chips / 1000000);
			if(c_a>10)
				ret = c_a + 'M';
			else {
				c_b = Math.round((chips-c_a*1000000)/100000);
				if(c_b==10){
					c_b=9;
				}
				ret = c_a + '.' + c_b + 'M';
			}
		}
		if(temp<0){
			ret +="";
			if(ret.indexOf("k")!=-1||ret.indexOf("M")!=-1){
				ret = "-"+ret;
			}else{
				ret = temp;
			}

		}
		return ret;
	},
	nextHand : function(){
		if(this.nextHandUrl){
			window.location.href = this.nextHandUrl;
		}
	},
	previousHand : function(){
		if(this.previousHandUrl){
			window.location.href = this.previousHandUrl;
		}
	},
	interval : {
		blindShow : 10,
		holecardShow : 10,
		preFlopShow : 5,
		flopShow : 5,
		turnShow : 5,
		riverShow : 5,
		showDown : 5,
		showCard : 5,
		bet : 10,
		showWait: 5,
		ins:5,
		end : 10,
	},
}

module.exports = {
	TimeControl: TimeControl,
	recordHelper: recordHelper
}
